//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Web.UI;
using System.Web.UI.WebControls;

using CommunityServer.Components;
using CommunityServer.Controls;
using CommunityServer.Discussions.Components;

namespace CommunityServer.Discussions.Controls.Search 
{
    // *********************************************************************
    //  SearchOptions
    //
    /// <summary>
    /// This server control renders and handles the search UI for the user.
    /// </summary>
    // ***********************************************************************/
    public class SearchOptions : TemplatedWebControl
    {
        #region Child Controls

        DropDownList forumList;
        DropDownList searchType;
        DropDownList matchType;
        Button searchButton;

        #endregion

        protected override void OnInit(EventArgs e) 
        {
            if (SkinName == null)                
                ExternalSkinFileName = "Skin-SearchOptions.ascx";
            else 
                ExternalSkinFileName = SkinName;
			
            base.OnInit(e);
        }
		
        protected override void OnLoad(EventArgs e) 
        {
            DataBind();

            base.OnLoad(e);
        }

        #region DataBind

        public override void DataBind() 
        {
            base.DataBind();
            
            if (null != forumList) 
            {
                forumList.DataSource = Forums.GetForums();
                forumList.DataTextField = "Name";
                forumList.DataValueField = "ForumID";
                forumList.DataBind();

                // Add the "All Forums" option if the user is allowed to search all forums
                //
                if (AllowSearchAllForums)
                    forumList.Items.Insert(0, new ListItem(CommunityServer.Components.ResourceManager.GetString("Search_SearchOptions_AllForums"), "-1"));
            }

            if (null != searchType) 
            {
                searchType.Items.Add(new ListItem(CommunityServer.Components.ResourceManager.GetString("Search_SearchOptions_Posts"), "0"));
                searchType.Items.Add(new ListItem(CommunityServer.Components.ResourceManager.GetString("Search_SearchOptions_PostedBy"), "1"));
            }

            if (null != matchType) 
            {
                matchType.Items.Add(new ListItem(CommunityServer.Components.ResourceManager.GetString("Search_SearchOptions_MatchAll"), "0"));
                matchType.Items.Add(new ListItem(CommunityServer.Components.ResourceManager.GetString("Search_SearchOptions_MatchAny"), "1"));
                matchType.Items.Add(new ListItem(CommunityServer.Components.ResourceManager.GetString("Search_SearchOptions_MatchExact"), "2"));	
            }

            if (null != searchButton) 
                searchButton.Text = ResourceManager.GetString("Search");
        }
		
        #endregion		
		
        #region Skin
                
        protected override void AttachChildControls() 
        {
            forumList = (DropDownList) FindControl("forumList");
            searchType = (DropDownList) FindControl("SearchType");
            matchType = (DropDownList) FindControl("MatchType");
            searchButton = (Button) FindControl("Search_Button");
			
            InitializeChildControls();
        }

        private void InitializeChildControls() 
        {
            if (null != searchButton) 
                searchButton.Click += new EventHandler( SearchButton_Click );
        }
		        
        #endregion

        #region Properties

        /// <summary>
        /// Does the control allow the searching of all forums
        /// </summary>
        [
        System.ComponentModel.DefaultValue( true ),
        ]
        public virtual Boolean AllowSearchAllForums 
        {
            get 
            {
                Object state = ViewState["AllowSearchAllForums"];
                if (state != null) 
                    return (Boolean) state;

                return true;
            }
            set 
            {
                ViewState["AllowSearchAllForums"] = value;
            }
        }

        #endregion

        #region Event Handlers

        // *********************************************************************
        //  SearchButton_Click
        //
        /// <summary>
        /// This event handler is called when the user clicks on the submit button,
        /// firing off the search.  It resets the DataGrid's CurrentPageIndex to 0
        /// and Displays the Search Results.
        /// </summary>
        // ***********************************************************************/
        private void SearchButton_Click(Object sender, EventArgs e) 
        {            
            // Exit if the page is invalid
            //
            if (!Page.IsValid) 
                return;
        }

        #endregion
    }
}